<script setup>
import router from "../../../../../router/index.js";
import { defineProps, ref, defineEmits } from "vue";
import { studentSignIn } from "../../../../../api/course/course.js";
import { isPermission } from "../../../../../utils/utils.js";
import { ElMessage } from "element-plus";
import AMapLoader from '@amap/amap-jsapi-loader';

const taskId = ref(router.currentRoute.value.params.id);
const props = defineProps(['isSignIn', 'signIn']);
const isSignIn = ref(props.isSignIn);
const emit = defineEmits(['close']);

const studentSignInInformation = ref({
  signId: '',
  type: '',
  address: '',
  signInNo: '',
});

const locationInfo = ref(null);

const handleCloseSignIn = () => {
  emit('close');
};

const handleSignIn = async () => {
  studentSignInInformation.value.signId = props.signIn.id;
  studentSignInInformation.value.type = props.signIn.type;
  if(props.signIn.type == 1){
    studentSignInInformation.value.address ="经度" +locationInfo.value.position.lng+"维度"+locationInfo.value.position.lat;
  }
  try {
    const res = await studentSignIn(studentSignInInformation.value);
    ElMessage.success("签到成功");
    emit('close');
  } catch (error) {
    ElMessage.error("签到失败，请重试");
    console.error(error);
  }
};

const getLocation = () => {
  if (!window.AMap) {
    console.error('AMap not loaded');
    return;
  }
  const geolocation = new AMap.Geolocation({
    enableHighAccuracy: true,
    timeout: 10000,
  });
  geolocation.getCurrentPosition((status, result) => {
    if (status === 'complete') {
      onComplete(result);
    } else {
      onError(result);
    }
  });
};

const onComplete = (result) => {
  console.log('定位成功');
  console.log('经纬度：', result.position);
  console.log('精度范围：', result.accuracy + '米');
  locationInfo.value = result;
};

const onError = (result) => {
  console.log('定位失败');
  console.log(result.message);
  ElMessage.error("定位失败，请重试");
};

</script>

<template>
  <el-dialog
      v-model="isSignIn"
      title="进行签到"
      width="500"
      align-center
      v-if="props.signIn"
  >
    <div v-if="props.signIn.type == 2">
      <div v-if="isPermission(0)">
        <span style="margin-right: 20px">签到码:</span>
        <el-input placeholder="请输入签到码" style="width: 120px"
                  v-model="studentSignInInformation.signInNo"></el-input>
      </div>
      <span v-if="isPermission(2)"
            style="font-size: 20px;display: flex;text-align: center">签到码为: {{ props.signIn.signInNo }}</span>
    </div>
    <div v-if="props.signIn.type == 1">
      <el-button @click="getLocation">获取位置</el-button>
      <div v-if="locationInfo">
        <p>定位成功</p>
        <p>经度: {{ locationInfo.position.lng }}</p>
        <p>纬度: {{ locationInfo.position.lat }}</p>
        <p>精度范围: {{ locationInfo.accuracy }} 米</p>
      </div>
      <div v-else>
        <p>尚未获取定位信息</p>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCloseSignIn">取消</el-button>
        <el-button type="primary" @click="handleSignIn" v-if="isPermission(0)">进行签到</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style scoped lang="less">
/* 根据需要添加样式 */
</style>
